<?php
class CategoryControl extends Control{   
    //显示栏目列表
    function index(){
        C("debug",0);
        $db  = m("category");
        // $total = $db->count(); //获得总条数
        // $page=new page($total,"10",3,2);
        // $result=$db->limit($page->limit())->all();
        $result=$db->where("pid=0")->order('cid ASC')->all();
        // p($result);
        // $page=$page->show();
        // $channel = Data::channel($result, "cid", "pid", 0, "", 2, "---");
        // p($channel);
        // P($channel);die;
        foreach ($result as $k => $v) {
            $result[$k]['child'] = $db->order('cid ASC')->all('pid = ' . $v['cid']);
        }
        // p($result);die;
        // $this->assign("page",$page);
        $this->assign("channel", $result);
        $this->display();
    }
    //添加栏目视图
    function addshow(){
        C("debug",0);
        $pid = isset($_GET['pid'])?$_GET['pid']:0;
        $this->assign("url",$_SERVER["HTTP_REFERER"]);
        $this->assign("pid",$pid);
        $this->display();
    }
    //写入数据库中
    function add(){
        C("debug",1);
        $db = M("category");
        if($db->add()){
            go("index");
        }else{
            $this->error("添加栏目失败");
        }
    }

    //编辑栏目视图
    function editshow() {
        C("debug",0);
        $cid = $this->_get("cid");
        if ($cid) {
            $db = M("category");
            $field = $db->find($cid);
            $this->assign("url",$_SERVER["HTTP_REFERER"]);
            $this->assign("field", $field); //分配栏目数据
            $this->display();
        }
    }

    //修改栏目数据
    function save() {
        $db = M("category");
        if ($db->save()) {
            $this->success("编辑栏目成功", "index");
        } else {
            $this->error("栏目修改失败");
        }
    }

    //删除栏目
    function delcategory() {
        $cid = $this->_get("cid");
        if ($cid) {
            $db = M("category");
            $data = array(); //返回客户端的数据
            if ($db->find("pid=" . $cid)) {
                $data['stat'] = "error";
                $data['msg'] = "请先删除子栏目";
            }else if ($db->table('arc')->find("cid=" . $cid)) {
                $data['stat'] = "error";
                $data['msg'] = "请先移除栏目内文章";
            } else {
                $db->del($cid);
                $data['stat'] = "success";
                $data['msg'] = "";
            }
            echo json_encode($data);exit;
        }
    }















}

?>